package com.zzuli.yxypre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzuli.yxypre.comment.R;
import com.zzuli.yxypre.entity.*;
import com.zzuli.yxypre.mapper.*;
import com.zzuli.yxypre.service.SingerService;
import com.zzuli.yxypre.service.TopService;
import com.zzuli.yxypre.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @auther pony
 * @create 2023-05-12 13:38
 */
@Service
public class TopServiceImpl extends ServiceImpl<SingerMapper, Singer> implements TopService {

    @Autowired
    private PlayCountMapper playCountMapper;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private TopMapper topMapper;

    @Autowired
    private LoveMapper loveMapper;

    @Autowired
    private AlbumMapper albumMapper;

    /**
     * 播放量统计
     * @param songId
     * @return
     */
    @Override
    public R playCount(Integer songId) {
        // 为歌曲添加播放量
        addCount(1, songId);
        // 为歌曲的专辑添加播放量
        List<Album> albumList = songMapper.selectAlbumBySongId(songId);
        Album album = albumList.get(0);
        Integer albumId = album.getAlbumId();
        addCount(2, albumId);
        // 为歌曲所在的歌单添加播放量
        List<Sheet> sheetList = songMapper.selectSheetBySongId(songId);
        for (Sheet sheet : sheetList) {
            addCount(3, sheet.getId());
        }
        return R.success().message("播放量添加成功");
    }

    /**
     * 获取飙升榜
     * @return
     */
    @Override
    public R soarList(HttpServletRequest request) {
        // 获取登录用户id
        Integer userId = Integer.valueOf(JwtUtils.getUserIdByJwtToken(request));
        LambdaQueryWrapper<Top> qw = new LambdaQueryWrapper<>();
        qw.eq(Top::getType, 1);
        List<Top> topList = topMapper.selectList(qw);
        ArrayList<Song> songList = new ArrayList<>();
        for (Top top : topList) {
            LambdaQueryWrapper<Song> songQw = new LambdaQueryWrapper<>();
            songQw.eq(Song::getSongId, top.getSongId());
            Song song = songMapper.selectOne(songQw);
            songList.add(song);
        }

        // 封装 歌曲信息
        for (Song song : songList) {
            // 判断歌曲是否被当前登录用户所喜欢
            LambdaQueryWrapper<Love> loveQw1 = new LambdaQueryWrapper<>();
            // 歌曲 type 是 1
            loveQw1.eq(Love::getType, 1);
            loveQw1.eq(Love::getUserId, userId);
            loveQw1.eq(Love::getRelevance, song.getSongId());
            Love love1 = loveMapper.selectOne(loveQw1);
            if (!Objects.isNull(love1)){
                song.setIsLove(1);
            }else {
                song.setIsLove(0);
            }

            // 封装歌曲的歌手信息
            // 查询歌手
            List<Singer> singerList = songMapper.selectSingerBySongId(song.getSongId());
            song.setSinger(singerList);
            // 查询专辑
            List<Album> albumList = songMapper.selectAlbumBySongId(song.getSongId());
            song.setAlbum(albumList);
        }
        return R.success().message("歌曲飙升榜").data("songList", songList);
    }

    /**
     * 获取热歌榜：每周更新
     * @return
     */
    @Override
    public R hotList(HttpServletRequest request) {
        // 获取登录用户id
        Integer userId = Integer.valueOf(JwtUtils.getUserIdByJwtToken(request));
        LambdaQueryWrapper<Top> qw = new LambdaQueryWrapper<>();
        qw.eq(Top::getType, 2);
        List<Top> topList = topMapper.selectList(qw);
        ArrayList<Song> songList = new ArrayList<>();
        for (Top top : topList) {
            LambdaQueryWrapper<Song> songQw = new LambdaQueryWrapper<>();
            songQw.eq(Song::getSongId, top.getSongId());
            Song song = songMapper.selectOne(songQw);
            songList.add(song);
        }

        // 封装 歌曲信息
        for (Song song : songList) {
            // 判断歌曲是否被当前登录用户所喜欢
            LambdaQueryWrapper<Love> loveQw1 = new LambdaQueryWrapper<>();
            // 歌曲 type 是 1
            loveQw1.eq(Love::getType, 1);
            loveQw1.eq(Love::getUserId, userId);
            loveQw1.eq(Love::getRelevance, song.getSongId());
            Love love1 = loveMapper.selectOne(loveQw1);
            if (!Objects.isNull(love1)){
                song.setIsLove(1);
            }else {
                song.setIsLove(0);
            }

            // 封装歌曲的歌手信息
            // 查询歌手
            List<Singer> singerList = songMapper.selectSingerBySongId(song.getSongId());
            song.setSinger(singerList);
            // 查询专辑
            List<Album> albumList = songMapper.selectAlbumBySongId(song.getSongId());
            song.setAlbum(albumList);
        }
        return R.success().message("歌曲热歌榜").data("songList", songList);
    }

    /**
     * 获取新歌榜：每周更新
     * @return
     */
    @Override
    public R newList(HttpServletRequest request) {
        // 获取登录用户id
        Integer userId = Integer.valueOf(JwtUtils.getUserIdByJwtToken(request));
        LambdaQueryWrapper<Top> qw = new LambdaQueryWrapper<>();
        qw.eq(Top::getType, 3);
        List<Top> topList = topMapper.selectList(qw);
        ArrayList<Song> songList = new ArrayList<>();
        for (Top top : topList) {
            LambdaQueryWrapper<Song> songQw = new LambdaQueryWrapper<>();
            songQw.eq(Song::getSongId, top.getSongId());
            Song song = songMapper.selectOne(songQw);
            songList.add(song);
        }

        // 封装 歌曲信息
        for (Song song : songList) {
            // 判断歌曲是否被当前登录用户所喜欢
            LambdaQueryWrapper<Love> loveQw1 = new LambdaQueryWrapper<>();
            // 歌曲 type 是 1
            loveQw1.eq(Love::getType, 1);
            loveQw1.eq(Love::getUserId, userId);
            loveQw1.eq(Love::getRelevance, song.getSongId());
            Love love1 = loveMapper.selectOne(loveQw1);
            if (!Objects.isNull(love1)){
                song.setIsLove(1);
            }else {
                song.setIsLove(0);
            }

            // 封装歌曲的歌手信息
            // 查询歌手
            List<Singer> singerList = songMapper.selectSingerBySongId(song.getSongId());
            song.setSinger(singerList);
            // 查询专辑
            List<Album> albumList = songMapper.selectAlbumBySongId(song.getSongId());
            song.setAlbum(albumList);
        }
        return R.success().message("歌曲新歌榜").data("songList", songList);
    }

    // 添加播放量
    void addCount(Integer type,Integer id){
        // 先查询数据库有没有记录
        LambdaQueryWrapper<PlayCount> playCountSelectQw = new LambdaQueryWrapper<>();
        playCountSelectQw.eq(PlayCount::getType, type);
        playCountSelectQw.eq(PlayCount::getRelevance, id);
        PlayCount selectResult = playCountMapper.selectOne(playCountSelectQw);
        PlayCount playCount = null;
        // 不存在就添加
        if (Objects.isNull(selectResult)){
            playCount = new PlayCount(null , id, type, 1, 1, 1, 1, 0);
            playCountMapper.insert(playCount);
        } else { // 存在
            playCount = new PlayCount(selectResult.getId(), id, type, selectResult.getSumCount()+1, selectResult.getDayCount()+1, selectResult.getWeekCount()+1, selectResult.getMonthCount()+1, selectResult.getYesterdayCount());
            playCountMapper.updateById(playCount);
        }
    }

    /**
     * 获取播放量前 5 的专辑数据
     * @return
     */
    @Override
    public R getBanner() {
        LambdaQueryWrapper<PlayCount> pcQw = new LambdaQueryWrapper<>();
        pcQw.eq(PlayCount::getType, 2);
        pcQw.orderByDesc(PlayCount::getWeekCount);
        pcQw.last("limit 0, 5");
        List<PlayCount> playCountList = playCountMapper.selectList(pcQw);
        ArrayList<Album> albumList = new ArrayList<>();
        for (PlayCount playCount : playCountList) {
            LambdaQueryWrapper<Album> albumQw = new LambdaQueryWrapper<>();
            albumQw.eq(Album::getAlbumId, playCount.getRelevance());
            Album album = albumMapper.selectOne(albumQw);
            albumList.add(album);
        }
        return R.success().message("播放量前5的歌单").data("albumList", albumList);
    }
}
